package com.knowif.backend.controller;

import com.github.pagehelper.PageInfo;
import com.knowif.backend.service.SchoolService;
import com.knowif.pojo.School;
import com.knowif.pojo.SchoolExample;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/5 0005 21:06
 */
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    @PostMapping("/save")
    public RestBean<?> save(@RequestBody School school){
        try {
            schoolService.saveSchool(school);
            return RestBean.build(RestBean.success_code);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition){
        try {
            SchoolExample schoolExample = ExampleUtil.buildExample(new SchoolExample(), searchCondition);
            List<School> schools = schoolService.selectSchool(schoolExample);
            PageInfo<School> pageInfo = new PageInfo<School>(schools);
            return RestBean.build(RestBean.success_code, "", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/queryNoPage")
    public RestBean<?> queryNoPage(@RequestBody SearchCondition searchCondition){
        try {
            SchoolExample schoolExample = ExampleUtil.buildExample(new SchoolExample(), searchCondition);
            List<School> schools = schoolService.selectSchool(schoolExample, false);
            return RestBean.build(RestBean.success_code, "", schools);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    /**
     * 单个删除
     * @param schoolId
     * @return
     */
    @DeleteMapping("/delete/{schoolId}")
    public RestBean<?> delete(@PathVariable("schoolId") int schoolId){
        try {
            schoolService.deleteSchool(schoolId);
            return RestBean.build(RestBean.success_code, "");
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    /**
     * 批量删除
     * @param searchCondition
     * @return
     */
    @PostMapping("/deleteAll")
    public RestBean<?> deleteAll(@RequestBody SearchCondition searchCondition){
        try {
            SchoolExample schoolExample = ExampleUtil.buildExample(new SchoolExample(), searchCondition);
            List<School> schools = schoolService.selectSchool(schoolExample, false);
            if(schools != null && schools.size() > 0){
                schools.forEach(school -> {
                    schoolService.deleteSchool(school.getId());
                });
            }
            return RestBean.build(RestBean.success_code, "");
        }catch (Exception e){
            return RestBean.buildError("参数错误");
        }
    }
}
